<template>
    <IContent :spinShow="loading">
        <ISearch v-model="search">
            <FormItem label="客户名称">
                <Input v-model="search.name" placeholder="客户名称" size="small"></Input>
            </FormItem>
            <FormItem label="客户代码">
                <Input v-model="search.code" placeholder="客户代码" size="small"></Input>
            </FormItem>
            <FormItem :label-width="1">
                <Button type="primary" icon="ios-search" @click="getLists(1)" size="small">搜索</Button>
                <Button type="success" icon="ios-add" @click="openComponent('ICreate')" size="small">添加</Button>
            </FormItem>
        </ISearch>
        <ITable :current="page.current" :table="table" :total="page.total" @on-page-change="pageChange">
            <template slot-scope="{ row, index }" slot="status">
                <Tag v-if="row.status === 0">草稿</Tag>
                <Tag v-if="row.status === 1">审核中</Tag>
                <Tag v-if="row.status === 2">审核通过</Tag>
                <Tag v-if="row.status === 3">冻结</Tag>
                <Tag v-if="row.status === 4">审核驳回</Tag>
            </template>
            <template slot-scope="{ row, index }" slot="assure">
                <Tag v-if="row.assure === 0">账册企业</Tag>
                <Tag v-if="row.assure === 1">电商企业</Tag>
                <Tag v-if="row.assure === 2">物流企业</Tag>
            </template>
            <template slot-scope="{ row, index }" slot="action">
                <Poptip transfer confirm title="确定要删除这条信息吗" @on-ok="destroy(row.id)">
                    <Button size="small" v-if="row.status === 0">删除</Button>
                </Poptip>
                <PoptipRemark title="解冻" size="small" type="default" @on-submit="unfrozen($event, row.id)" v-if="row.status === 3"></PoptipRemark>
                <PoptipRemark title="冻结" size="small" type="default" @on-submit="frozen($event, row.id)" v-if="row.status === 2"></PoptipRemark>
                <Button size="small" v-if="row.status === 0" @click="openComponent('IUpdate', {id: row.id})">修改</Button>
                <Button size="small" v-if="row.status === 4" @click="openComponent('IUpdate', {id: row.id})">修改</Button>
                <Button size="small" @click="openComponent('IShow', {id: row.id})">查看</Button>
            </template>
        </ITable>

        <component v-if="componentProps.value" :is="componentProps.view" v-model="componentProps.value" :props="componentProps.props"
                   @input="getLists(page.current)"></component>
    </IContent>
</template>

<script>
    import contentListPage from "../../../mixins/contentListPage";
    import IContent from "../../../components/content/index";
    import ISearch from "../../../components/content/search";
    import ITable from "../../../components/content/table";
    import contentDrawer from "../../../mixins/contentDrawer";
    import ICreate from "./create";
    import IUpdate from "./update";
    import IShow from "./show";
    import PoptipRemark from "../../../components/content/poptipRemark";

    export default {
        name: "index",
        components: {PoptipRemark, ITable, ISearch, IContent, ICreate, IUpdate, IShow},
        mixins: [contentListPage, contentDrawer],
        data() {
            return {
                table: {
                    columns: [
                        {
                            title: '客户代码',
                            key: 'code'
                        },
                        {
                            title: '公司名称',
                            key: 'name'
                        },
                        {
                            title: '联系电话',
                            key: 'phone'
                        },
                        {
                            title: '联系人',
                            key: 'contacts'
                        },
                        {
                            title: '税金交付企业',
                            slot: 'assure'
                        },
                        {
                            title: '状态',
                            slot: 'status'
                        },
                        {
                            title: '操作',
                            slot: 'action'
                        }
                    ]
                },
                search: {}
            }
        },
        methods: {
            getLists(page) {
                this.loading = true;
                this.$http.get(`customer/index`, {
                    params: Object.assign({}, this.search, {page: page})
                }).then((data) => {
                    this.table.data = data.data
                    this.page.total = data.total
                    this.page.current = data.current_page
                }).finally(() => {
                    this.loading = false;
                });
            },
            destroy(id) {
                this.loading = true;
                this.$http.delete(`customer/index/${id}`).then(() => {
                    this.getLists(this.page.current);
                }).finally(() => {
                    this.loading = false;
                });
            },
            frozen(value, id) {
                this.loading = true;
                this.$http.put(`customer/${id}/frozen`, {value}).then((res) => {
                    this.getLists(this.page.current);
                }).finally(() => {
                    this.loading = false;
                });
            },
            unfrozen(value, id) {
                this.loading = true;
                this.$http.put(`customer/${id}/unfrozen`, {value}).then((res) => {
                    this.getLists(this.page.current);
                }).finally(() => {
                    this.loading = false;
                });
            }
        }
    }
</script>

<style scoped>

</style>
